import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class ContentExchange extends Thread{
	
	private Socket socket = null;
	private String ip="";
	
    public ContentExchange(Socket socket) throws IOException {
        this.socket = socket;
        ip=socket.getInetAddress().getHostAddress();
        //System.out.println(ip+" "+ip.length());
        
    }

	
	public void run(){
		String filename="";
		DataInputStream din1;
		DataOutputStream dout1,dout2;
        try {
        	din1= new DataInputStream(socket.getInputStream());
			dout2=new DataOutputStream(socket.getOutputStream());
			
			//BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			//Receiving from client
		
			filename=iLink.num+din1.readUTF();
			//filename=br.readLine();//+;
			//while(filename==null){
				//filename=br.readLine();
			System.out.println(filename);
			File g=new File(filename);
			dout1=new DataOutputStream(new FileOutputStream(g));
			int ch1;
			long length=din1.readLong();
			System.out.println(length);
			for(long lo=0;lo<length;lo++)
			{
				ch1=din1.read();
				dout1.write(ch1);
			}				
			dout1.close();    
			XML_Receiver xr=new XML_Receiver(filename,ip);
			XML_Generator xg=new XML_Generator();
			//g.delete();
			//Writing to client
			//File f6=new File("structure.xml");
			//FileInputStream fi=new FileInputStream(f6);
			//PrintStream p = new PrintStream(socket.getOutputStream());
			//dout2.writeUTF(f6.getName());
			//dout2.writeLong(f6.length());
			//p.println(f6.getName());
			//System.out.print(f6.length()+"\n");
			/*int c;
			int k3=0;
			while((c=fi.read())!=-1)
			{
				dout2.write(c);
				dout2.flush();
				k3++;
			}	
			System.out.print(k3);
			dout2.close();*/
			//fi.close();


		} catch (IOException e) {
			e.printStackTrace();
		} 	
		
		
	}
}
